package com.api.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.core.ioc.annotation.Configuration;
import cn.hserver.core.ioc.annotation.Value;

/**
 * @author hxm
 */
@Configuration
public class RedissonConfig {

    @Value("redis.address")
    private String address;

    @Value("redis.password")
    private String password;

    @Value("redis.database")
    private Integer database;

    @Bean
    public RedissonClient redissonClient() {
        try {
            Config config = new Config();
            if (password != null) {
                config.useSingleServer().setAddress("redis://" + address).setDatabase(database).setPassword(password);
            } else {
                config.useSingleServer().setAddress("redis://" + address).setDatabase(database);
            }
            return Redisson.create(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}